import Vue from "vue";
import router from './router'

let vm = new Vue();

router.beforeEach(async (to,from,next)=>{

    /* 1.path为login时，不拦截 */
    if(to.path == '/login'){
        next()
    /* 2.拦截 */
    }else{
        let token = localStorage.getItem('token');
        /* 2.1 没有token或者为空跳转到登录页 */
        if(token==null || token==''){
            next('/login')
        /* 2.2 存在token则发送请求到后端验证 */
        }else{
            const {data} = await vm.$request({
                url:`/user/info/${token}`
            })
            if(data.flag){
                next()
            }else{
                next('/login')
            }
        }
    }
})